package enemies;


import com.jmex.audio.AudioSystem;
import com.jmex.audio.AudioTrack;

import mapControl.GraphicsMapControl;
import net.datastructures.Position;
import elementos.Elemento;
import enemies.strategy.Strategy;

/** "Product" */
public class Enemy extends Elemento implements Runnable {

	private Strategy strategy;
	private boolean live;

	GraphicsMapControl mapControl;
	private Position<Enemy> pos;

	public Enemy(GraphicsMapControl mapControl) {
		this.mapControl = mapControl;
		mapControl.getRootNode().attachChild(node);
		live = true;
	}

	public void setStrategy(Strategy strategy) {
		this.strategy = strategy;

	}

	public boolean executeStrategy(){
		return strategy.execute();
		
	}

	
	public void locate(int row, int col) 
	{
		this.setRow(row);
		this.setCol(col);
		node.setLocalTranslation(mapControl.getTraslationCentrada(row, col));
		pos = mapControl.getCelda(row, col).addEnemy(this);
	}
			
			
	public void moverA(int row, int col) {
		
		int lastRow = getRow();
		int lastCol = getCol();
		
		this.setRow(row);
		this.setCol(col);
		node.setLocalTranslation(mapControl.getTraslationCentrada(row, col));
			
		mapControl.getCelda(lastRow, lastCol).removeEnemy(this);
		pos = mapControl.getCelda(row, col).addEnemy(this);

	}

	@Override
	public void run() {
		while (live)
		{
			try {
				live = executeStrategy();
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

	public void kill() {
		live = false;
		AudioSystem audio = AudioSystem.getSystem();
		AudioTrack targetSound = audio.createAudioTrack(getClass().getResource(
        "/music/cry.wav"), false);
		targetSound.setVolume(1.0f);	
		targetSound.play();
		mapControl.removeEnemy(this);
		
	}

	public Position<Enemy> getPos() {
		return pos;
	}




	
}